package demo.letcode.top100;

/**
 * todo
 * 给n个非负整数a1,a2..an,每个数代表坐标中的一个点(i,ai).在坐标内画n条垂直线，
 * 垂直线i的两个端点分别为(i,ai)和(i,0)。找出其中的两条线，
 * 使得他们与x轴共同构成的容器可以容纳最多的水
 * <p>
 * 输入[1,8,6,2,5,4,8,3,7]
 * 输出49
 *
 * @author taqo
 * @date 2021/5/7
 */
public class _盛最多的水 {
    public static void main(String[] args) {
        int a[] = {1, 8, 6, 2, 5, 4, 8, 3, 7};
        int len = a.length;
        int area = 0;
        int maxArea = 0;
        for (int i = 0; i < len - 1; i++) {
            for (int j = i + 1; j < len; j++) {
                area = Math.min(a[i], a[j]) * (j - i);
                maxArea = Math.max(area, maxArea);
            }
        }
        System.out.println(maxArea);
        a(a);
    }

    public static void a(int a[]) {
        // int a[] = {1, 8, 6, 2, 5, 4, 8, 3, 7};
        int maxArea = 0, left = 0, right = a.length - 1;
        int area = 0;
        while (left < right) {
            area = Math.min(a[left], a[right]) * (right - left);
            maxArea = Math.max(area, maxArea);

            // 柱子矮的往中间靠
            if (a[left] < a[right]) {
                //
                left++;
            } else {
                right--;
            }
        }
        System.out.println(maxArea);
    }

    public static void b(int a[]) {
        // int a[] = {1, 8, 6, 2, 5, 4, 8, 3, 7};
        int left = 0, right = a.length - 1, area = 0;
        int maxArea = 0;

        while (left < right) {
            area = Math.min(a[left], a[right]) * (left - right);
            maxArea = Math.max(area, maxArea);

            if (a[left] < a[right]) {
                left++;

            } else {
                right--;
            }
        }
    }
}
